﻿using System;
using System.Collections.Generic;
using System.Text;

namespace iToolPlugin.SMSProvider
{
    public class HTTPRequest
    {
        public string body;

        public Encoding bodyEncoding;

        public Dictionary<string, string> headers;

        public Dictionary<string, string> parameters;

        public int connectTimeout;

        public int requestTimeout;

        public HTTPMethod method
        {
            get;
            set;
        }

        public string url
        {
            get;
            set;
        }

#pragma warning disable CS8618 // 在退出构造函数时，不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
        public HTTPRequest(HTTPMethod method, string url)
#pragma warning restore CS8618 // 在退出构造函数时，不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
        {
            this.method = method;
            this.url = url;
            headers = new Dictionary<string, string>();
            parameters = new Dictionary<string, string>();
            bodyEncoding = Encoding.UTF8;
        }

        public HTTPRequest setBody(string body)
        {
            this.body = body;
            return this;
        }

        public HTTPRequest setBodyEncoding(Encoding bodyEncoding)
        {
            this.bodyEncoding = bodyEncoding;
            return this;
        }

        public HTTPRequest addHeader(string name, string value)
        {
            headers.Add(name, value);
            return this;
        }

        public HTTPRequest addQueryParameter(string name, string value)
        {
            parameters.Add(name, value);
            return this;
        }

        public HTTPRequest addQueryParameter(string name, int value)
        {
            parameters.Add(name, value.ToString());
            return this;
        }

        public HTTPRequest addQueryParameter(string name, long value)
        {
            parameters.Add(name, value.ToString());
            return this;
        }

        public HTTPRequest setConnectionTimeout(int connectionTimeout)
        {
            connectTimeout = connectionTimeout;
            return this;
        }

        public HTTPRequest setRequestTimeout(int requestTimeout)
        {
            this.requestTimeout = requestTimeout;
            return this;
        }

        public override string ToString()
        {
            return $"url: {url}, headers: {headers.ToString()}, body: {body}";
        }
    }
}
